package by.bsuir.csschat.ui;
import by.bsuir.csschat.messages.AttachmentMessage; 
import java.awt.event.ActionEvent; 
import java.awt.event.MouseEvent;

import javax.swing.*;
public class frmRecvFiles extends javax.swing.JFrame {
    
    private AttachmentMessage []_files;
    
    public frmRecvFiles(AttachmentMessage []iArr) {
        initComponents();
        _files=iArr;
        ltFiles.setModel(new FileListModel(_files));//список принятых файлов
        setVisible(true);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mnuPopup = new javax.swing.JPopupMenu();
        mnuSaveas = new javax.swing.JMenuItem();
        panSplit = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        ltFiles = new javax.swing.JList();
        scrollArea = new javax.swing.JScrollPane();
        tbFileContent = new javax.swing.JTextArea();

        mnuSaveas.setLabel("Save as...");
        mnuSaveas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuSaveasActionPerformed(evt);
            }
        });
        mnuPopup.add(mnuSaveas);

        setTitle("Received Files");
        setName("frmRecvFiles"); // NOI18N
        getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.LINE_AXIS));

        panSplit.setDividerLocation(100);
        panSplit.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        ltFiles.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ltFilesMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(ltFiles);

        panSplit.setTopComponent(jScrollPane1);

        tbFileContent.setBackground(new java.awt.Color(204, 204, 204));
        tbFileContent.setColumns(20);
        tbFileContent.setRows(5);
        scrollArea.setViewportView(tbFileContent);

        panSplit.setRightComponent(scrollArea);

        getContentPane().add(panSplit);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mnuSaveasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuSaveasActionPerformed
       if(ltFiles.getSelectedIndex()>=0){//индекс выбранного файла
           AttachmentMessage curSel=(AttachmentMessage)ltFiles.getSelectedValue();
            java.io.File oFile=FileDialog.SaveFileDialog();//создание нового диалога
            
            if(oFile==null)
                return;
            try {
                curSel.SaveToFile(oFile.getPath());
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this,ex.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
                ex.printStackTrace();
            }
            
        }
    }//GEN-LAST:event_mnuSaveasActionPerformed

    private void ltFilesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ltFilesMouseClicked
        if(evt.getButton()==java.awt.event.MouseEvent.BUTTON1){
            if(ltFiles.getSelectedIndex()>=0){
                AttachmentMessage curSel=(AttachmentMessage)ltFiles.getSelectedValue();
                tbFileContent.setText(curSel.GetContent());
            }
        }else{
            mnuPopup.show(this,evt.getX(),evt.getY());
        }
}//GEN-LAST:event_ltFilesMouseClicked
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList ltFiles;
    private javax.swing.JPopupMenu mnuPopup;
    private javax.swing.JMenuItem mnuSaveas;
    private javax.swing.JSplitPane panSplit;
    private javax.swing.JScrollPane scrollArea;
    private javax.swing.JTextArea tbFileContent;
    // End of variables declaration//GEN-END:variables
    
}

class FileListModel extends javax.swing.AbstractListModel{
    AttachmentMessage[] _list;
    public FileListModel(AttachmentMessage[] iList){
        _list=iList;
    }
    public int getSize() { return _list.length; }
    public Object getElementAt(int i) { return _list[i]; }
}